package com.xv.yan.dao.impl;

import com.xv.yan.beans.entity.StudentDo;
import com.xv.yan.beans.req.StudentRequest;
import com.xv.yan.beans.res.TableResult;
import com.xv.yan.beans.vo.StudentVo;
import com.xv.yan.dao.StudentDao;
import com.xv.yan.util.DBUtil;
import com.xv.yan.util.StringUtil;

import java.sql.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    @Override
    public int addStudent(StudentDo studentDo) {
        // 获取数据库连接
        Connection connection = DBUtil.getConnection();
        try {
            // sql语句 占位符
            String sql = "insert into student (studentName, no, birthDay) value(?,?,?)";
            // 执行sql语句
            PreparedStatement ps = connection.prepareStatement(sql);
            // 给占位符传值
            ps.setString(1,studentDo.getStudentName());
            ps.setString(2, studentDo.getNo());
            ps.setObject(3, studentDo.getBirthDay());
            // 执行操作，返回的i是数据库中受影响的行数
            int i = ps.executeUpdate();
            return i;
        }catch (Exception e){
            e.printStackTrace();
        }
        // 执行不成功  返回0，我刚才好不容易把他那个看懂  误人子弟    傻逼似的  用字符串拼接。。。
        return 0;
        //哇，你写的好简洁啊，   给我讲讲
    }

    @Override
    public TableResult<StudentVo> queryStudentByPage(StudentRequest studentRequest) {
        TableResult<StudentVo> tableResult=new TableResult<>();
        Connection connection=DBUtil.getConnection();
        if (connection == null){
            return null;
        }
        PreparedStatement ps=null;
        ResultSet rs=null;
        StringBuilder sb=new StringBuilder();
        sb.append("select * from student ");
        appendWhere(studentRequest,sb);
        sb.append("order by id desc limit ").append(studentRequest.getStart())
                .append(",").append(studentRequest.getPageSize());

        try {
            ps=connection.prepareStatement(sb.toString());
            if (StringUtil.isNotBlank(studentRequest.getStudentName())){
                ps.setObject(1,studentRequest.getStudentName());
            }
            //打印最终的sql语句

            System.out.println("queryStudentByPage查询学生的sql:"+ps.toString());
            rs=ps.executeQuery();
            List<StudentVo> list=new ArrayList<>();
            while (rs.next()){
                StudentVo studentVo = buildStudent(rs);
                list.add(studentVo);
            }
            tableResult.setDate(list);//设置数据    咋一会儿vo  一会儿do的？ do的时候显示的是时间戳，然后新建了一个类vo
            //查询总条数
            sb.setLength(0);
            sb.append("select count(*) from student ");
            appendWhere(studentRequest,sb);
            ps=connection.prepareStatement(sb.toString());
            if (StringUtil.isNotBlank(studentRequest.getStudentName())){
                ps.setObject(1,studentRequest.getStudentName());
            }
            //打印最终的sql语句
            System.out.println("queryStudentByPage查询学生总条数的sql:"+ps.toString());
            rs=ps.executeQuery();
            if (rs.next()){
                int anInt = rs.getInt(1);
                tableResult.setTotalCount(anInt);//设置总条数
            }
            return tableResult;


        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private StudentVo buildStudent(ResultSet rs) throws SQLException, ParseException {
        int id=rs.getInt("id");
        String studentName = rs.getString("studentName");
        String no = rs.getString("no");
        String birthDay = rs.getString("birthDay");
        StudentVo studentVo=new StudentVo();
        studentVo.setId(id);
        studentVo.setStudentName(studentName);
        studentVo.setNo(no);
        studentVo.setBirthDay(birthDay);
        return studentVo;
    }

    private void appendWhere(StudentRequest studentRequest, StringBuilder sb) {
        String studentName=studentRequest.getStudentName();
        if (StringUtil.isNotBlank(studentName)){
            sb.append("where studentName = ? ");
        }
    }
}
